
String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function Check_Mobiles(theForm) 
{
    return /^((13|15|18|14)+\d{9})$/.test(theForm);
}

function Check_Tel(theForm) 
{
    return /^((\d{3,4})\-{0,1}){0,1}(\d{7,8})$/.test(theForm);
} 

function Check_Meth(nodevalue)
{ 
    var checkOK = "0123456789";
	var checkStr = nodevalue.trim();
	var allValid = true;
	var validGroups = true; 
	for (i = 0;  i < checkStr.length;  i++)
    { 
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
        { 
            if (ch == checkOK.charAt(j))
                break;
            if (j == (checkOK.length-1))
            {
            allValid = false;
            break;
            }
        }
    } 
    return allValid;
}

function setProvince()
{
	Ts_areaInfo.getAreaClassByUpId('000000000000000',function(data){
		DWRUtil.removeAllOptions('province');
		DWRUtil.addOptions('province',{'':'请选择'});
		DWRUtil.addOptions('province',data);
	})
}
function setCitys(prov)
{
   var provence=prov;
	 Ts_areaInfo.getAreaClassByUpId(provence,function(data){
		DWRUtil.removeAllOptions('eparchy_code');
		DWRUtil.addOptions('eparchy_code',{'':'请选择'});
		DWRUtil.addOptions('eparchy_code',data);
		DWRUtil.removeAllOptions('city_code');
		DWRUtil.addOptions('city_code',{'':'请选择'});
	});
}

function setAreas(city_id){
	Ts_areaInfo.getAreaClassByUpId(city_id,function(data){
	 	DWRUtil.removeAllOptions('city_code');
    DWRUtil.addOptions('city_code',{'':'请选择'});
	 	DWRUtil.addOptions('city_code',data);
	});
}




 function ChecktheForm_Name()
 { 
    var consignee = document.getElementById("consignee");
	if(consignee.value.trim()=="")
	{   
		
        document.getElementById("consignee_ck").style.display ="block";
		consignee.focus();
		return false;
	} 
	else
	{
	   document.getElementById("consignee_ck").style.display ="none";
	} 
	return true;
 }
 
 //验证省市区
 function ChecktheForm_PCA()
 {
    
	DWREngine.setAsync(false);//设为同步   
	var province = document.getElementById("province").value;
	var flag = true;
	if(province.trim() == "")
	{   
		document.getElementById("address_error_name").innerHTML ="请选择省份/直辖市";
		document.getElementById("address_ck").style.display ="block";
		return false;
	}
	else
	{
	     var eparchy_code = document.getElementById("eparchy_code").value
		 if(eparchy_code == "")
		 {
			 Ts_areaInfo.checkSonArea(province,function(bool){
				 if(bool)
				 {
					document.getElementById("address_error_name").innerHTML ="请选择市";
		            document.getElementById("address_ck").style.display ="block";
				    flag = false;
				 }
				 else
				 {
				     flag = true;
				 }
			 });
	   }
	   else
	   {
	        var city_code = document.getElementById("city_code").value
			if(city_code == "")
			{
				 Ts_areaInfo.checkSonArea(eparchy_code,function(bool){
					 if(bool)
					 {
						document.getElementById("address_error_name").innerHTML ="请选择县/区";
						document.getElementById("address_ck").style.display ="block";
						flag = false;
					 }
					 else
					 {
					  flag = true;
					 }
				 });
		    }
	    }
		
	}
	if(flag)
	{
	  document.getElementById("address_ck").style.display ="none";
	}
	DWREngine.setAsync(true);   
    return flag;
 }
 
 
 function ChecktheForm_Address()
 {  
    var Address = document.getElementById("address");
    if(Address.value.trim()=="")
	{ 
	    document.getElementById("detail_addr").style.display ="block";
		Address.focus();
		return false;
	}
	else
	{
	    document.getElementById("detail_addr").style.display ="none";
	} 
	return true;
  }
 
 function ChecktheForm_Zip()
  {
    var Zip = document.getElementById("post_code");
    if(Zip.value.trim()=="")
	{
	    document.getElementById("post_error_name").innerHTML ="请输入邮政编码";
		document.getElementById("post_code_ck").style.display ="block";
		Zip.focus();
		return false;
	}
	else if(!Check_Meth(Zip.value.trim()))
	{
	    document.getElementById("post_error_name").innerHTML ="邮政编码必须为数字";
		document.getElementById("post_code_ck").style.display ="block";
		Zip.focus();
		return false;
	}
	else if(Zip.value.trim().length!=6)
	{
	    document.getElementById("post_error_name").innerHTML ="邮政编码必须为6位";
		document.getElementById("post_code_ck").style.display ="block";
	    Zip.focus();
	    return false;			    	
	}
	else
	{
	    document.getElementById("post_code_ck").style.display ="none";
	} 
	return true;
  }
  
   function ChecktheForm_Tel() {
      var Tel = document.getElementById("tel");
      var Mobile = document.getElementById("cellphone");
      if (Mobile.value.trim()!="" && Check_Mobiles(Mobile.value.trim()) == false) {
          document.getElementById("contact_error_name").innerHTML ="请输入正确的手机号码";
		  document.getElementById("contact_ck").style.display ="block";
          Mobile.focus();
          return false;
      }
      else if (Mobile.value.trim() != "" && Mobile.value.trim().length != 11) {
          document.getElementById("contact_error_name").innerHTML ="手机号码必须11位";
		  document.getElementById("contact_ck").style.display ="block";
          Mobile.focus();
          return false;
      }
      else 
	  {
            document.getElementById("contact_ck").style.display ="none";
      }

      if (Tel.value.trim().length > 18) {
          document.getElementById("contact_error_name").innerHTML ="电话号码过长";
		  document.getElementById("contact_ck").style.display ="block";
          Tel.focus();
          return false;
      }
      else if (Tel.value.trim() != "" &&  !/^[0][1-9]{2,4}-[0-9]{5,8}$/.test(Tel.value.trim())) {
		  document.getElementById("contact_error_name").innerHTML ="请输入正确的电话号码";
		  document.getElementById("contact_ck").style.display ="block";
          Tel.focus();
          return false;
      }
      else 
	  {
           document.getElementById("contact_ck").style.display ="none";
      }

      if (Tel.value.trim() == "" && Mobile.value.trim() == "") {
           document.getElementById("contact_error_name").innerHTML ="手机和电话至少有一项必填";
		   document.getElementById("contact_ck").style.display ="block";
		   Tel.focus();
           return false;
      }
      
      return true;
  }
  



function Check_ConsigneeForB2c()
{
    if(!ChecktheForm_Name()) return; 
 	if(!ChecktheForm_PCA()) return;  
	if(!ChecktheForm_Address()) return; 
	if(!ChecktheForm_Zip()) return;
	if(!ChecktheForm_Tel()) return; 
	var area_attr = "";
	if(document.getElementById("province").value != "")
	{
	   area_attr += document.getElementById("province").value+"|";
	   if(document.getElementById("eparchy_code").value !="")
	   {
	      area_attr += document.getElementById("eparchy_code").value +"|";
		  if(document.getElementById("city_code").value !="")
		  {
		      area_attr += document.getElementById("city_code").value +"|";
		  }
	   }
	}
	document.getElementById("area_attr").value = area_attr;
    document.addrForm.submit();

}


function setAddress(consignee,area_attr,address,post_code,tel,cellphone)
{
    document.getElementById("put_consignee").value = consignee;
	document.getElementById("put_area_attr").value = area_attr;
	document.getElementById("put_address").value = address;
	document.getElementById("put_post_code").value = post_code;
	document.getElementById("put_tel").value = tel;
	document.getElementById("put_cellphone").value = cellphone;
	document.putForm.submit();

}

function setAddress2(consignee,area_attr,address,post_code,tel,cellphone)
{
    document.getElementById("put_consignee").value = consignee;
	document.getElementById("put_area_attr").value = area_attr;
	document.getElementById("put_address").value = address;
	document.getElementById("put_post_code").value = post_code;
	document.getElementById("put_tel").value = tel;
	document.getElementById("buy_mobile").value = cellphone;
	alert("配送地址已填写完毕");
}


function ModifyAddr(consignee,area_attr,address,post_code,tel,cellphone,addr_id)
{
    document.getElementById("consignee").value = consignee;
	
	if(area_attr !="")
	{
	   var attrStr = area_attr.split('|');
	   DWREngine.setAsync(false);//设为同步   
	   if(attrStr.length > 0)
	   {
	        Ts_areaInfo.getAreaClassByUpId('000000000000000',function(data){
				DWRUtil.removeAllOptions('province');
				DWRUtil.addOptions("province",{'':'--请选择--'});
				DWRUtil.addOptions('province',data);
				for (var i = 0; i < document.getElementById('province').options.length; i++) 
				{       
				   if(document.getElementById('province').options[i].value == attrStr[0]) 
				   {        
					   document.getElementById('province')[i].selected = true;        
				   }      
				} 
		    });
	   }
	   if(attrStr.length > 1)
	   {
	        Ts_areaInfo.getAreaClassByUpId(attrStr[0],function(data){
					DWRUtil.removeAllOptions('eparchy_code');
					DWRUtil.addOptions("eparchy_code",{'':'--请选择--'});
					DWRUtil.addOptions('eparchy_code',data);
					DWRUtil.removeAllOptions('city_code');
					DWRUtil.addOptions('city_code',{'':'--请选择--'});
					for (var i = 0; i < document.getElementById('eparchy_code').options.length; i++) 
					{       
				      if (document.getElementById('eparchy_code').options[i].value == attrStr[1]) 
				      {        
				           document.getElementById('eparchy_code')[i].selected = true;  
				      }      
			    }

		    });
	   }
	   if(attrStr.length > 2)
	   {
	        Ts_areaInfo.getAreaClassByUpId(attrStr[1],function(data){
		 	    DWRUtil.removeAllOptions('city_code');
			    DWRUtil.addOptions("city_code",{'':'--请选择--'});
		 	    DWRUtil.addOptions('city_code',data);	
			 	for (var i = 0; i < document.getElementById('city_code').options.length; i++) 
			 	{       
				   if (document.getElementById('city_code').options[i].value == attrStr[2]) 
				   {        
					   document.getElementById('city_code')[i].selected = true;    
				   }      
				}
		    });
	   }
	   DWREngine.setAsync(true);
	}

	document.getElementById("address").value = address;
	document.getElementById("post_code").value = post_code;
	document.getElementById("tel").value = tel;
	document.getElementById("cellphone").value = cellphone;
	document.getElementById("g_addr_id").value = addr_id;
	

}


function setShow(val)
{
   if(document.getElementById(val).style.display =='none')
   {
       document.getElementById(val).style.display ='block'; 
	   document.getElementById(val+"_shou").style.background ='url(/templets/order/images/icon_error.gif) no-repeat 25px center';
	   /*
	   if(val=="invoiceDiv")
	   {
	      document.getElementById("open_invoice1").checked = false;
		  document.getElementById("open_invoice2").checked = true;
		  document.getElementById('inv_div').style.display ='none';
	   }
	   */
   }
   else
   {
       document.getElementById(val).style.display ='none'; 
	   document.getElementById(val+"_shou").style.background ='url(/templets/order/images/icon_add.jpg) no-repeat 25px center';
	    /*
		 if(val=="invoiceDiv")
		 {
		   document.getElementById("open_invoice1").checked = false;
		   document.getElementById("open_invoice2").checked = true;
		   document.getElementById('inv_div').style.display ='none';
		 }
	   */
   }

}

function subOrder()
{
	 if(document.getElementById("leavelWord").style.display == "block")
   {
      if(trim(document.getElementById("o_mem_message").value) == "")
	  {
	     alert('请输入留言内容！');
         return;	
	  }
       
   }
   
   if(document.getElementById("invoiceDiv").style.display == "block")
   {
      if(trim(document.getElementById("invoicetop").value) == "")
	  {
	    alert('请输入发票抬头！');
        return;	
	  }
	  
	   document.getElementById("order_if_need_invoice").value = "0";
	   document.getElementById("order_invoicetop").value =   document.getElementById("invoicetop").value;
	   document.getElementById("order_invoicecontent").value =   document.getElementById("invoicecontent").value;
	   
	
   }
   var o_mem_message = document.getElementById("o_mem_message").value;
   if(o_mem_message.length > 300)
   {
      o_mem_message =  o_mem_message.subtring(0,300);
   }
   document.getElementById("order_mem_message").value = o_mem_message;
   document.orderForm.submit();
}

function subcustOrder(cust_id,shipingfeetotal,packfee,custtotalcost,custtotalnoofferprice,order_inter_num,goods_name,cuse_vmoney,index)
{
	document.getElementById("cust_id").value=cust_id;
	document.getElementById("shipingfeetotal").value=shipingfeetotal;
	document.getElementById("order_shipping_fee").value=shipingfeetotal;
	document.getElementById("subject").value=goods_name;
	document.getElementById("packfee").value=packfee;
	document.getElementById("all_total_amount").value=custtotalcost;
	document.getElementById("all_goods_amount").value=parseFloat(custtotalcost)-parseFloat(packfee);
	document.getElementById("no_offer_total_amount").value=custtotalnoofferprice;
	document.getElementById("order_inter_num").value=order_inter_num;

	if(document.getElementById("use_vmoney_"+index).checked){
		if(parseFloat(custtotalcost)>=parseFloat(cuse_vmoney))
			document.getElementById("all_user_money").value=cuse_vmoney;
		else
			document.getElementById("all_user_money").value=custtotalcost;
	}else{
		document.getElementById("all_user_money").value="0";
	}
	
	 if(document.getElementById("leavelWord").style.display == "block")
   {
      if(trim(document.getElementById("o_mem_message").value) == "")
	  {
	     alert('请输入留言内容！');
         return;	
	  }
       
   }
   
   if(document.getElementById("invoiceDiv").style.display == "block")
   {
      if(trim(document.getElementById("invoicetop").value) == "")
	  {
	    alert('请输入发票抬头！');
        return;	
	  }
	  
	   document.getElementById("order_if_need_invoice").value = "0";
	   document.getElementById("order_invoicetop").value =   document.getElementById("invoicetop").value;
	   document.getElementById("order_invoicecontent").value =   document.getElementById("invoicecontent").value;
	   
	
   }
   var o_mem_message = document.getElementById("o_mem_message").value;
   if(o_mem_message.length > 300)
   {
      o_mem_message =  o_mem_message.subtring(0,300);
   }
   document.getElementById("order_mem_message").value = o_mem_message;
 //  alert(document.getElementById("all_goods_amount").value);
   document.orderForm.submit();
}




